package com.yu1996.search.controller;

import com.yu1996.search.pojo.Article;
import com.yu1996.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * @param article 索引对象
     * @return entity.Result
     * @description 添加索引
     * @date 2019/10/15 19:01
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article) {
        articleService.save(article);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    @RequestMapping(value = "/search/{keywords}/{page}/{size}", method = RequestMethod.GET)
    public Result findTitlePage(@PathVariable String keywords, @PathVariable int page, @PathVariable int size) {
        Page<Article> titlePage = articleService.findTitlePage(keywords, page, size);
        return new Result(true, StatusCode.OK, "搜索成功", new PageResult<>(titlePage.getTotalElements(), titlePage.getContent()));
    }
}
